<?php

/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

class rss {

    function rss(){}

    function lastestArticle($limit=20) {
        global $db;
        $sql = "SELECT title,link,image,short_description FROM article WHERE status='yes' ORDER BY create_date DESC LIMIT $limit";
        return $db->query($sql);
    }

    function getArticlesByCattegoryLink($link) {
        global $db, $config;
        
        if ($link == 'new') {
            $newSQL = "SELECT title,link,short_description,image,create_date FROM article WHERE status='yes' ORDER BY create_date DESC LIMIT " . $config ['total_article_on_category'];
            $rsArticles = $db->query($newSQL);
            return array(
                "cat_name" => 'Tin mới nhất',
                "cat_link" => 'new',
                "rsArticles" => $rsArticles
            );
        } else {
            $sqlGetCatId = "SELECT id,name,link FROM article_category WHERE link='$link'";
            $rsGetCatId = $db->query_first($sqlGetCatId);
            if (intval($rsGetCatId['id']) > 0) {
                $arrCatId = get_cat_id_to_arr($rsGetCatId['id']);
                $str_cat_id = '';
                foreach($arrCatId as $key=>$arrValue){
                    if ($str_cat_id=='') {
                        $str_cat_id .= $arrCatId[$key]['id'];
                    } else {
                        $str_cat_id .= $arrCatId[$key]['id'] . ',';
                    }
                }
                if ($str_cat_id == '') {
                    $str_cat_id = $rsGetCatId['id'];
                    $sql = "SELECT count(a.id) as numrows FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE c.link = '$link' AND a.status='yes' ORDER BY a.create_date DESC";
                } else {
                    $str_cat_id = $str_cat_id . $rsGetCatId['id'];
                    $sql = "SELECT count(a.id) as numrows FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE c.link = '$link' OR c.id in ($str_cat_id) AND a.status='yes' ORDER BY a.create_date DESC";
                }
                $rsTotalRows = $db->query_first($sql);
                $totalRows = $rsTotalRows['numrows'];
                $totalPages = ceil($totalRows / $config ['total_article_on_category']);
                $start = (($config ['page'] * $config ['total_article_on_category']) - $config ['total_article_on_category']);
                $limit = $config ['total_article_on_category'];

                if ($str_cat_id == '') {
                    $sql = "SELECT a.id,a.cat_id,a.link,a.title,a.image,a.short_description,a.description,a.create_date,c.link as cat_link,c.name as cat_name FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE c.link = '$link' AND a.status='yes' ORDER BY a.create_date DESC LIMIT $start,$limit";
                } else {
                    $sql = "SELECT a.id,a.cat_id,a.link,a.title,a.image,a.short_description,a.description,a.create_date,c.link as cat_link,c.name as cat_name FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE c.link = '$link' OR c.id in ($str_cat_id) AND a.status='yes' ORDER BY a.create_date DESC LIMIT $start,$limit";
                }
                $rsArticles = $db->query($sql);
                return array(
                    "str_cat_id" => $str_cat_id,
                    "cat_name" => $rsGetCatId['name'],
                    "cat_link" => $rsGetCatId['link'],
                    "totalRows" => $totalRows,
                    "rsArticles" => $rsArticles
                );
            } else {
                header("Location: " . $config['domain']);
            }
        }
    }

    function get_cat_id_to_arr($parent_id = 0) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT * FROM article_category WHERE  parent_id = " . intval($parent_id));
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
            }
        }
        return $trees;
    }

    function getHousingByCategory($limit) {
        global $db;
        global $config;
        $sqlCheck = "SELECT parent_id FROM housing_category WHERE id=" . intval($config['id']);
        $rsCheck = $db->query_first($sqlCheck);
        if ($rsCheck['parent_id'] != 0) {
            $sqlTotal = "SELECT id, title, price,price_unit,price_type,description
                    FROM housing h
                    WHERE
                    h.cat_id=" . $config['id'] . " ORDER BY h.create_date DESC";

            $totalRows = $db->num_rows($db->query($sqlTotal));
            $totalPages = ceil($totalRows / $config ['total_house_on_category']);
            $start = (($config ['page'] * $config ['total_house_on_category']) - $config ['total_house_on_category']);

            $sql = "SELECT id, title, price,price_unit,price_type,description
                FROM housing h
                WHERE
                h.cat_id=" . $config['id'] . " ORDER BY h.create_date DESC LIMIT $start,$limit";
            return Array("rs" => $db->query($sql), "totalRows" => $totalRows);
        } else {
            $sqlTotal = "SELECT id, title, price,price_unit,price_type,description
                    FROM housing h ORDER BY h.create_date DESC";

            $totalRows = $db->num_rows($db->query($sqlTotal));
            $totalPages = ceil($totalRows / $config ['total_house_on_category']);
            $start = (($config ['page'] * $config ['total_house_on_category']) - $config ['total_house_on_category']);

            $sql = "SELECT id, title, price,price_unit,price_type,description
                FROM housing h ORDER BY h.create_date DESC LIMIT $start,$limit";
            return Array("rs" => $db->query($sql), "totalRows" => $totalRows);
        }
    }

    function getLastestArticlesByCategoryId($categoryId) {
        global $db;
        $arrCatId = get_cat_id_to_arr($categoryId);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }
        if ($str_cat_id == '') {
            $sql = "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=" . intval($categoryId) . " AND a.status='yes' ORDER BY a.create_date DESC";
        } else {
            $sql = "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE (a.cat_id=" . intval($categoryId) . " OR a.cat_id IN ($str_cat_id)) AND a.status='yes' ORDER BY a.create_date DESC";
        }

        $rs = $db->query_first($sql);
        return $rs;
    }

    function getArticleByCategoryIdOffsetLimit($categoryId, $offset=0, $limit=10) {
        global $db;
        $arrCatId = get_cat_id_to_arr($categoryId);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }
        if ($str_cat_id == '') {
            $sql = "SELECT id,title,link FROM article WHERE cat_id=" . intval($categoryId) . " AND status='yes' ORDER BY create_date DESC LIMIT $offset,$limit";
        } else {
            $sql = "SELECT id,title,link FROM article WHERE (cat_id=" . intval($categoryId) . " OR cat_id IN ($str_cat_id)) AND status='yes' ORDER BY create_date DESC LIMIT $offset,$limit";
        }

        $rs = $db->query($sql);
        return $rs;
    }

    function ortherArticleInCategory($create_date, $strCategoryId, $limit=10) {
        global $db, $config;
        $sql = "SELECT title,link FROM article WHERE create_date < '$create_date' AND cat_id IN ($strCategoryId) AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rs = $db->query($sql);
        return $rs;
    }

}

?>
